package com.pedro.streamer.view;

import android.content.Context;
import android.os.Environment;
import android.util.AttributeSet;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;


import com.pedro.streamer.R;
import com.pedro.streamer.manager.MediaPlayerClient;
import com.pedro.streamer.utils.FileUtils;

import java.io.File;
import java.io.IOException;

public class VideoSoundView extends ConstraintLayout {


    private CheckBox soundCheckBox;
    private SeekBar soundSeekBar;
    private TextView volumeText;
    private Context mContext;


    public VideoSoundView(@NonNull Context context) {
        super(context);
        mContext = context;
    }

    public VideoSoundView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        initView();
        MediaPlayerClient.getInstance(mContext).setVideoSyncListen(new MediaPlayerClient.videoSyncListen() {
            @Override
            public void videoSync() {
                syncDate();
            }
        });
    }



    private void initView() {
        soundCheckBox = findViewById(R.id.video_sound_switch);

        soundCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!compoundButton.isPressed()) {
                    return;
                }

                File play_sound_switch = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera1/no-silent.jpg");
                if (play_sound_switch.exists() != b) {
                    if (b) {
                        try {
                            play_sound_switch.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else {
                        play_sound_switch.delete();
                    }
                }

            }


        });


        soundSeekBar = findViewById(R.id.video_volume_seek_bar);
        soundSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                volumeText.setText(String.valueOf(i));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                FileUtils.setVolume(String.valueOf(seekBar.getProgress()));
            }
        });

        volumeText = findViewById(R.id.volume_text);


        syncDate();


    }


    private void sync_statue_with_files() {


        File camera_dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera1");
        if (!camera_dir.exists()) {
            camera_dir.mkdir();
        }


        File videoFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/VideoManager");
        if (!videoFile.exists()) {
            videoFile.mkdir();
        }


        File disable_file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera1/disable.jpg");
//        videoSwitch.setChecked(!disable_file.exists());

//        File force_show_file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera1/force_show.jpg");
//        force_show_switch.setChecked(force_show_file.exists());

        File play_sound_file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera1/no-silent.jpg");
        soundCheckBox.setChecked(play_sound_file.exists());


        String volumeStr = FileUtils.getVolume();
        volumeText.setText(volumeStr);
        soundSeekBar.setProgress(Integer.valueOf(volumeStr));

//        File force_private_dir_file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera1/private_dir.jpg");
//        force_private_dir.setChecked(force_private_dir_file.exists());

//        File disable_toast_file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera1/no_toast.jpg");
//        disable_toast_switch.setChecked(disable_toast_file.exists());

    }

    public void syncDate(){

        if (volumeText != null){
            sync_statue_with_files();
        }

    }
}
